<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
</head>
<script type="text/javascript" src="./script/aui-tab.js"></script>
<script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>

<body>

    <header class="head" style="padding-top:0.4rem;">
        <div class="head-l" tapmode onclick="closeWin()"><i class="fa fa-angle-left"></i></div>
        <h3>地址管理</h3>
        <div class="head-r" id="delText" onclick="handleDel();">删除</div>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>
    <div id="list"></div>
    <!--         <label class="duihao"><i class="active"></i></label>
        <dl class="address-item-m">
            <dt>王丽丽<span>18822345678</span></dt>
            <dd>辽宁省 沈阳市 东陵区 白塔堡街道 爱尚丽家小区 一单元 12号 1401门 </dd>
        </dl>
        <div class="address-item-r"><img src="image/address-item-r.png"></div> -->

        <div class="address-btn-t"></div>
        <div class="address-btn">
            <div style="text-align: center;" id="addBtn" class="btn-1" tapmode onclick="address_add()">添加收货地址</div>
            <div style="display: none;text-align: center;" id="delBtn" class="btn-1" tapmode onclick="goDelete()">删除</div>
        </div>

    </body>
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/index.js"></script>
    <script type="text/template" id="template">
        {{~it:value:index}}
        <div class="address-item">
            <label {{? value.is_default}} style="display: block;" id="theDefault" {{??}} style="display: none;" {{?}} class="duihao"><i class="checkBtn
                {{? value.is_default == 1}}
                active
                {{?}}
                " id="{{=value.id}}"></i></label>
                <dl class="address-item-m" onclick="callback({{=value.id}})">
                    <dt>{{=value.name}}<span>{{=value.phone}}</span></dt>
                    <!-- <dd>辽宁省 沈阳市 东陵区 白塔堡街道 爱尚丽家小区 一单元 12号 1401门 </dd> -->
                    <dd>{{=value.district[0].title}} {{=value.district[1].title}} {{=value.district[2].title}} {{=value.address}} </dd>
                </dl>
                <div class="address-item-r" onclick="editAddress({{=value.id}});"><img src="image/address-item-r.png"></div>
            </div>
            {{~}}
        </script>
        <script type="text/javascript">
            apiready = function() {
        // 添加监听事件 (页面刷新)
        delStatus = 0;
        reloadListener();
        loadData();
    }

    function loadData() {
        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/address/index',
            method: 'get',
            data: {
                values: {},
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        }, function(ret, err) {
            if (ret) {
                // alert( JSON.stringify( ret ) );
                templateFun(ret.data);

            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data) {
        if (data.length) {
            var template = $api.byId('template');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list');
            $api.html(list, html);
        } else {

            var list = $api.byId('list');
            var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无内容</h2>';
            list.innerHTML = html;
        }
    }

    // 点击删除按钮
    function handleDel() {
        delStatus = !delStatus;
        if (delStatus == 1) {
            $('#delText').text('完成');
            $('#delBtn').css('display', 'block');
            $('#addBtn').css('display', 'none');
            $('.duihao').css('display', 'block');
            $('.checkBtn').removeClass('active');
            $('.checkBtn').click(function() {

                $(this).toggleClass("active");
            })
        } else {
            $('#delText').text('删除');
            $('#delBtn').css('display', 'none');
            $('#addBtn').css('display', 'block');
            $('.duihao').css('display', 'none');
            // 默认地址 显示对号
            $('#theDefault').css('display', 'block');
            // 默认地址对号 选中状态
            $('.checkBtn').addClass('active');
            // 移除对号选中事件
            $(".checkBtn").unbind("click");
        }
    }

    // 执行删除操作
    function goDelete() {
        var ele = $('.checkBtn.active');
        var idArr = Array();
        ele.each(function() {
            var id = $(this).attr('id');
            idArr.push(id);
        })
            // alert(idArr);
            var ids = idArr.join(',');
        // alert(ids);
        // return false;
        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/address/remove',
            method: 'post',
            data: {
                values: {
                    ids: ids
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        }, function(ret, err) {
            if (ret) {
                // alert( JSON.stringify( ret ) );
                if (ret.status_code == 200) {
                    api.toast({
                        msg: ret.message,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                handleDel();
                loadData();
            } else {
                // alert( JSON.stringify( err ) );
                if (statusCode == 400) {
                    api.toast({
                        msg: err.body.message,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return false;
                }
            }
        });

    }

    function callback(id) {
        var name = api.pageParam.name;
        // if (name != 'shop_order_details') {
        //     return;
        // }

        var back_script = 'changeAdress('+id+');';
        api.execScript({
            name: 'shop_order_details',
            script: back_script
        });

        var back_script = 'changeAdress('+id+');';
        api.execScript({
            name: 'shop_month_order_details',
            script: back_script
        });

        api.closeWin();
    }

    // 编辑按钮
    function editAddress(id) {
        api.openWin({
            name: 'address_edit',
            url: './address_edit.html',
            pageParam: {
                id: id
            }
        });
    }

    // 监听事件 (页面刷新)
    function reloadListener() {
        api.addEventListener({
            name: 'reload',
        }, function(ret, err) {
            loadData();
        });
    }

    // 打开添加地址
    function address_add() {
        api.openWin({
            name: 'address_add',
            url: './address_add.html',
        });
    }

    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
